<template>
    <!-- 表单 -->
    <el-form inline>
        <el-form-item label="教师名称" style="width: 20%;">
            <el-input v-model="params.nickName" placeholder="请输入要搜索教师名称" />
        </el-form-item>
        <el-form-item label="教师头衔" style="width: 20%;">
            <el-input v-model="params.title" placeholder="请输入要搜索的教师头衔" />
        </el-form-item>
        <el-form-item>
            <el-button @click="onSearch" plain>搜索</el-button>
            <el-button @click="onReset" plain type="info">重置</el-button>
            <el-button @click="onAddTeacher" plain type="success">新增</el-button>
        </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table height="526px" :data="teacherList">
        <el-table-column prop="teacherId" label="教授id" />
        <el-table-column prop="nickName" label="教师名称" />
        <el-table-column prop="title" label="教师头衔" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="creatorName" label="创建人账号" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" width="200px" fixed="right">
            <template #default="{ row }">
                <el-button type="text" @click="onEdit(row.teacherId)">编辑
                </el-button>
                <el-button type="text" class="red" @click="onDelete(row.teacherId)">删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination background size="small" layout="total, sizes, prev, pager, next, jumper" :total="total"
        v-model:current-page="params.pageNum" v-model:page-size="params.pageSize" :page-sizes="[5, 10, 20, 50]"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    <teacher-drawer ref="teacherEditRef" @success="onSuccess"></teacher-drawer>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { getTeacherListService, delTeacherService } from '@/apis/teacher'
import TeacherDrawer from "@/components/drawer/TeacherDrawer.vue"

const teacherEditRef = ref()
const params = reactive({
    pageNum: 1,
    pageSize: 10,
    nickName: '',
    title: ''
})

const teacherList = ref([])
const total = ref(0)

async function getTeacherList() {
    const ref = await getTeacherListService(params)
    teacherList.value = ref.rows
    total.value = ref.total
}
getTeacherList()


function onSearch() {
    params.pageNum = 1
    getTeacherList()
}

function onReset() {
    params.pageNum = 1
    params.pageSize = 10
    params.nickName = ''
    params.title = ''
    getTeacherList()
}


function handleSizeChange(newSize) {
    params.pageNum = 1
    getTeacherList()
}

function handleCurrentChange(newPage) {
    getTeacherList()
}


// 添加教师
const onAddTeacher = () => {
    teacherEditRef.value.open()
}


function onSuccess(service) {
    if (service === 'add') {
        params.pageNum = 1
    }
    getTeacherList()
}

async function onDelete(teacherId) {
    const isConfirmed = window.confirm("确定要删除该教师吗？");
    if (isConfirmed) {
        // 用户点击确认
        await delTeacherService(teacherId);
        params.pageNum = 1;
        getTeacherList();
    } else {
        // 用户点击取消
        console.log("删除操作已取消");
    }
}
async function onEdit(teacherId) {
    teacherEditRef.value.open(teacherId)
}
</script>